package com.asiainfo.iam.task;


import com.asiainfo.iam.constant.VerificationConstant;
import com.asiainfo.iam.entity.dto.VerifyDTO;
import com.asiainfo.iam.mapper.DataVerifyMapper;
import com.asiainfo.iam.tool.VerifyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class VerifyTask {
    @Autowired
    private DataVerifyMapper dataVerifyMapper;
    @Autowired
    private VerifyUtil verifyUtil;

public Map<Integer, Integer> verifyTask() {

    // 查询所有待验证数据
    List<VerifyDTO> verifyDTOS = dataVerifyMapper.selectAllVerifyData();
    // 用来存放验签成功和失败的结果
        Map<Integer, Integer> resultMap = new HashMap<>();
    // 对待验证数据进行bs64编码
    for (VerifyDTO verifyDTO : verifyDTOS) {

        String base64EncodedString = Base64.getEncoder().encodeToString(verifyDTO.getUserPassword().getBytes());
        verifyDTO.setOriginalBase64(base64EncodedString);
    }
    for(VerifyDTO verifyDTO : verifyDTOS){
        //自研算法
        long verify = verifyUtil.verifySignature(verifyDTO.getOriginalBase64(), verifyDTO.getB64SignedData());
        if (verify == 0){
            verifyDTO.setResult(0);
            resultMap.put(verifyDTO.getUserId(), 0);
        }else {
            verifyDTO.setResult(1);
            resultMap.put(verifyDTO.getUserId(), 1);
        }
    }
    dataVerifyMapper.updateVerifyResult(verifyDTOS);



    return resultMap;
}

}
